package org.chen.ctrip.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.chen.common.result.ResultVO;
import org.chen.common.upload.BackgroundResource;
import org.chen.ctrip.respones.UploadResponse;
import org.chen.ctrip.service.upload.impl.UploadServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private UploadServiceImpl uploadService;

    /**
     * 上传背景资源到HeyGen
     */
    @PostMapping("/background")
    @Tag(name = "上传文件", description = "上传资源到存储服务器")
    public UploadResponse uploadBackground(@RequestParam Map<String, MultipartFile> files) {
        // 提取所有 MultipartFile
        List<MultipartFile> fileList = files.values()
                .stream()
                .filter(file -> file != null && !file.isEmpty())
                .collect(Collectors.toList());

        if (fileList.isEmpty()) {
            return UploadResponse.fail("insert error message");
        }

        try {
            List<BackgroundResource> uploadedResources = uploadService.uploadMultipleFiles(fileList);
            return UploadResponse.success(uploadedResources); // 返回数组
        } catch (Exception e) {
            return UploadResponse.fail("insert error message");
        }
    }

}
